package dao

import (
	"context"
	"gorm.io/gorm"
	"time"
)

var UserNotFoundErr = gorm.ErrRecordNotFound

type UserDao struct {
	Db *gorm.DB
}

func (dao *UserDao) Insert(ctx context.Context, u User) error {
	now := time.Now().UnixMilli()
	u.Ctime = now
	u.Mtime = now
	return dao.Db.WithContext(ctx).Create(&u).Error
}
func (dao *UserDao) SelectByEmail(ctx context.Context, email string) (User, error) {
	var u User
	tx := dao.Db.WithContext(ctx).First(&u, "email = ?", email)
	return u, tx.Error
}

type User struct {
	Id       int64  `gorm:"primary_key"`
	Email    string `gorm:"type:varchar(255);unique"`
	Password string `gorm:"size:255"`
	Ctime    int64  `gorm:"index"`
	Mtime    int64  `gorm:"index"`
}
